import {
  BASE_URL
} from '../../apis/requests.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchName: "",
    books: []
  },

  inputChange(e) {
    this.setData({
      searchName: e.detail.value
    })
  },  
  
  search() {
    if (this.data.searchName.length === 0) {
      return
    } else {
      const values = {
        "token": wx.getStorageSync('token'),
        "id": this.data.searchName
      }
      wx.request({
        url: BASE_URL + "/admin/find_order_by_id",
        data: values,
        header: {
          'content-type': 'application/json'
        },
        method: 'POST',
        dataType: 'json',
        success: (result) => {
          console.log(result)
          if (result.data.code === "200") {
            this.setData({
              books: result.data.data
            })

          }
        },
        fail: () => {},
        complete: () => {}
      })
    }
  },

  bindBorrow(e) {
    console.log(e.currentTarget.dataset.index)
    const values = {
      "token": wx.getStorageSync('token'),
      "id": this.data.searchName,
      "book_id": this.data.books[e.currentTarget.dataset.index].book_id
    }

    wx.request({
      url: BASE_URL + "/admin/borrow_book",
      data: values,
      header: {
        'content-type': 'application/json'
      },
      method: 'POST',
      dataType: 'json',
      success: (result) => {
        console.log(result)
        if (result.data.code === "200") {
          wx.showModal({
            title: "提示",
            content: "操作成功！",
            showCancel: false,
            success(res) {
              wx.navigateBack({
                complete: (res) => {},
              })
            }
          })
        } else {
          wx.showModal({
            title: "提示",
            content: result.data.msg,
            showCancel: false,
            success(res) {
            }
          })
        }
      },
      fail: () => {},
      complete: () => {}
    })


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})